package com.lanou.dllo.baisibudejie.adapater;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.lanou.dllo.baisibudejie.R;
import com.lanou.dllo.baisibudejie.db.MySQLiteMyNewsUtil;
import com.lanou.dllo.baisibudejie.entity.ImageEntity;
import com.lanou.dllo.baisibudejie.db.entity.Sql_MyNewsEntity;
import com.lanou.dllo.baisibudejie.view.CircleImage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/31.
 */
public class RecommendAdapter extends RecyclerView.Adapter<RecommendAdapter.RecommendViewHolder> {

    private Context context;
    private List<ImageEntity> datas;
    private RequestQueue queue;
    private boolean isClick;
    private MySQLiteMyNewsUtil loginUtil;
    private String name;
    private String uri;
    private Sql_MyNewsEntity sql_myNewsEntity;



    public RecommendAdapter(Context con) {
        this.context = con;
        loginUtil = new MySQLiteMyNewsUtil(con);
        queue = Volley.newRequestQueue(con);
    }

    /**
     * 加载行布局
     *
     * @param parent
     * @param viewType
     * @return
     */

    @Override
    public RecommendViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.recommend_item, null);
        return new RecommendViewHolder(view);
    }

    /**
     * 解析数据设置内容
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(final RecommendViewHolder holder, int position) {
        final ImageEntity entity = datas.get(position);
        if (entity != null) {
            holder.position = position;
            if (entity.getName() != null && entity.getName().length() > 0) {
                name = entity.getName();
                holder.recommend_nameTv.setText(entity.getName());
            }
            if (entity.getProfile_image() != null && entity.getProfile_image().length() > 0) {
                uri = entity.getProfile_image();
                getCircleBitmap(entity.getProfile_image(), holder.recommend_imgIv);
            }
//            holder.recommend_followIv.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
////                    holder.recommend_followIv.setImageBitmap(person_profile_add_attention_normal);
//                    if(isClick){
//                        holder.recommend_followIv.setImageResource(R.mipmap.person_profile_add_attention_normal);
//                        isClick = false;
//
//                        sql_myNewsEntity.setName(name);
//                        sql_myNewsEntity.setImageUrl(uri);
//                        loginUtil.saveNewsDatasToDataBase(sql_myNewsEntity);
//
//                    }else{
//                        holder.recommend_followIv.setImageResource(R.mipmap.person_profile_cancle_attention_normal);  //设置imageview呈现的图片
//                        isClick = true;
//                    }
//                }
//
//            });
        }
    }


    @Override
    public int getItemCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    public void addData(List<ImageEntity> data) {
        this.datas = data;
        notifyDataSetChanged();//数据改变 页面刷新
    }

    class RecommendViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView recommend_imgIv, recommend_followIv;
        private TextView recommend_nameTv;
        private int position;

        public RecommendViewHolder(View view) {
            super(view);
            recommend_imgIv = (ImageView) view.findViewById(R.id.recommend_imgIv);
            recommend_nameTv = (TextView) view.findViewById(R.id.recommend_nameTv);
            recommend_followIv = (ImageView) view.findViewById(R.id.recommend_followIv);
            sql_myNewsEntity = new Sql_MyNewsEntity();
            recommend_followIv.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            //取到当前点击行布局得内容
            ImageEntity imageEntity = datas.get(position);
            if (isClick) {
                recommend_followIv.setImageResource(R.mipmap.person_profile_add_attention_normal);
                isClick = false;
                String name = imageEntity.getName();
                String imageUrl = imageEntity.getProfile_image();
                Sql_MyNewsEntity sql_myNewsEntity = new Sql_MyNewsEntity(name, imageUrl);
//                sql_myNewsEntity.setName(name);
//                sql_myNewsEntity.setImageUrl(imageUrl);
                loginUtil.saveNewsDatasToDataBase(sql_myNewsEntity);
            } else {
                recommend_followIv.setImageResource(R.mipmap.person_profile_cancle_attention_normal);  //设置imageview呈现的图片
                isClick = true;
            }

        }
    }

    private void getCircleBitmap(String url, final ImageView imageView) {

        ImageRequest request = new ImageRequest(url,
                new Response.Listener<Bitmap>() {

                    @Override
                    public void onResponse(Bitmap response) {
                        int radius;
                        // 将返回的bitmap图片对象设置到图片组件中
                        if (response.getWidth() <= response.getHeight()) {
                            radius = response.getWidth() / 2;
                        } else {
                            radius = response.getHeight() / 2;
                        }

                        Bitmap circleImage = new CircleImage(context).getCroppedRoundBitmap(response, radius);
                        imageView.setImageBitmap(circleImage);
                    }
                },
                0, 0, Bitmap.Config.ARGB_8888,
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub
                        error.printStackTrace();
                    }
                }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                // TODO Auto-generated method stub
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        queue.add(request);//将本次图片请求加入到请求队列中
    }
}
